package cn.lcfms.app.admin.controller;


import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.lcfms.app.admin.bean.ItemBean;
import cn.lcfms.bin.BaseCache;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;

@PermitPoll(type="系统功能",name="栏目管理")
@Controller("admin.ItemController")
@RequestMapping("/admin/item")
public class ItemController extends AdminBaseController {
	/*
	 * 管理栏目页面
	 */
	@RequestMapping("/manage")
	public ModelAndView manage(){
		ModelAndView view=new ModelAndView("admin/frame/itemManage");	
		return view;
	}
	@RequestMapping(value="/getItemNameById",produces="text/html;charset=utf-8")
	@ResponseBody
	public String getItemNameById(int itemId){
		BaseService service = Application.getService("item");
		ItemBean bean=service.selectOne(ItemBean.class, itemId);
		if(null==bean.getItemName()){
			return "0";
		}
		return bean.getItemName();
	}
	
	
	@ResponseBody
	@RequestMapping(value="/getTree",produces="text/html;charset=utf-8")
	public String getTree(int parentId,HttpServletRequest request){
		BaseService service = Application.getService("item");
		List<HashMap<String, Object>> item = service.order("sort").selectList();
		String tree = new Tree(item, parentId).tree;	
		return tree;
	}
	
	private class Tree {
		private List<HashMap<String, Object>> item;
		private String tree = "";

		private Tree(List<HashMap<String, Object>> item, int parentId) {
			this.item = item;
			this.getTree(parentId);
		}

		private void getTree(int parentId) {
			this.tree += "<ol class=\"dd-list\" parentId=\"" + parentId + "\">";
			List<HashMap<String, Object>> list = this.item;
			for (int i = 0; i < list.size(); i++) {
				if ((int) list.get(i).get("parentId") == parentId) {
					this.tree += "<li class=\"dd-item dd2-item\" data-id=\""+ list.get(i).get("itemId")+ "\">"
							  +  "<div class=\"dd-handle dd2-handle\">"+list.get(i).get("itemId")+"</div>";						
					String url="";
					if (null!=list.get(i).get("url") && !list.get(i).get("url").equals("")) {
						url= "&nbsp;&nbsp;&nbsp;&nbsp;<a data-rel=\"tooltip\" data-placement=\"top\" title=\""+list.get(i).get("url")+"\" href=\"javascript:void(0);\" class=\"tooltip-info no-hover-underline\"><i class=\"iconfont icon-tiaozhuan size-14\"></i></a>";
					}
					this.tree += "<div class=\"dd2-content\">"	
							+ "<span class=\"col-xs-6\">&nbsp;&nbsp;"+ list.get(i).get("itemName")					
							+ url
							+ "</span>"
							+ "<div class=\"pull-right action-buttons\">"					
							+ "<a class=\"blue tooltip-info\" href=\"additem?parentId="+ list.get(i).get("itemId")+ "\" data-rel=\"tooltip\" data-placement=\"top\" data-original-title=\"添加子栏目\"><i class=\"iconfont icon-jia size-14\"></i></a>&nbsp;"
							+ "<a class=\"blue tooltip-info\" href=\"update_view?itemId="+ list.get(i).get("itemId")+ "\" data-rel=\"tooltip\" data-placement=\"top\" data-original-title=\"编辑\"><i class=\"iconfont icon-edit size-14\"></i></a>&nbsp;"
							+ "<a class=\"red tooltip-info\" href=\"javascript:deleteItem("+ list.get(i).get("itemId")+ ");\"  data-rel=\"tooltip\" data-placement=\"top\" data-original-title=\"删除\"><i class=\"iconfont icon-lajitong size-14\"></i></a>&nbsp;"
							+ "</div>"
							+ "</div>";					
					this.getTree((int) list.get(i).get("itemId"));
					this.tree += "</li>";
				}
			}
			this.tree += "</ol>";
			this.tree = this.tree.replaceAll("<ol class=\"dd-list\" parentId=\"\\d+\"></ol>","");
		}
	}
	/*
	 * 删除栏目与其子栏目
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(int itemId){
		BaseService service = Application.getService("item");
		this.deleteall(itemId,service);
		set_item_cache();
		return "1";
	}

	/*
	 * 递归删除栏目
	 */
	private void deleteall(int id,BaseService service){
		service.deleteById(id);
		service.where("parentId=#{parentId}").setData(id);
		List<HashMap<String, Object>> list = service.selectList();
		for (int i = 0; i < list.size(); i++) {
			int itemId = (int) list.get(i).get("itemId");
			this.deleteall(itemId,service);			
		}
	}
	/*
	 * 更新栏目排序
	 */
	@RequestMapping(value="/sort",produces="text/html;charset=utf-8")
	@ResponseBody
	public String sort(ItemBean item,HttpServletResponse response){
		BaseService service = Application.getService("item");
		service.where("itemId="+item.getItemId());
		service.setData(item.getSort(),item.getParentId());
		service.update("sort","parentId");		
		List<HashMap<String, Object>> list = BaseCache.ITEMCACHE;
		for (int i = 0; i < list.size(); i++) {
			if ((int) list.get(i).get("itemId") == item.getItemId()) {
				return (String)list.get(i).get("itemName");
			}
		}
		set_item_cache();
		return "栏目ID" + item.getItemId() + "[尚未缓存栏目名称]";
	}
	
	/*
	 * 增加或修改栏目
	 */
	@RequestMapping("/add_update_item")
	public String add_update_item(int[] rid,ItemBean bean){
		int itemId = bean.getItemId();
		BaseService service = Application.getService("item");
		if (itemId == 0) {			
			service.insert(bean);
			itemId=service.insert_id();
		} else {
			service.update(bean,"sort");
		}
		if(itemId!=0 && null!=rid && rid.length>0) {
			service.setTable("permit");
			service.where("item", itemId).delete();
			for(int r:rid) {
				service.setData(r,itemId).insert("rid","item");
			}
		}
		set_item_cache();
		return "local:manage";
	}
	
	/*
	 * 增加栏目页面
	 */
	@RequestMapping("/additem")
	public ModelAndView additem(ItemBean item){
		ModelAndView view=new ModelAndView("admin/frame/itemAdd");
		BaseService service = Application.getService("role");
		List<HashMap<String, Object>> list = service.selectList();
		view.addObject("grouplist", list);
		view.addObject("item", item);
		service.setTable("item");
		service.sql("select max(sort) as sort from item where parentId="+item.getParentId());
		HashMap<String, Object> map = service.getResult().get(0);
		if(null!=map) {
			Object sort=map.get("sort");
			view.addObject("sort", sort);
		}else {
			view.addObject("sort", 1);
		}	
		return view;
	}
	
	/*
	 * 修改单个栏目
	 */
	@RequestMapping("/update_view")
	public ModelAndView update_view(int itemId){
		BaseService service = Application.getService("item");
		ItemBean item = service.selectOne(ItemBean.class,itemId);
		ModelAndView view=new ModelAndView("admin/frame/itemAdd");
		service.setTable("role");
		List<HashMap<String, Object>> list = service.selectList();
		view.addObject("grouplist", list);
		view.addObject("item", item);	
		service.setTable("permit");
		List<HashMap<String, Object>> permit = service.where("item", item.getItemId()).column("rid").selectList();		
		view.addObject("permit", permit);	
		return view;	
	}
}
